﻿//二维码图片
Ext.ux.qrcodeImage = Ext.extend(Ext.BoxComponent, {
	autoRefresh: true,
	initComponent: function () {
		this.autoEl = {
			tag: 'a',
			href: 'javascript:void(0);',
			children: {
				tag: 'img',
				src: this.src
			}
		}
		Ext.ux.qrcodeImage.superclass.initComponent.call(this)
	},
	onRender: function (ct, position) {
		Ext.ux.qrcodeImage.superclass.onRender.call(this, ct, position)
		if (this.autoRefresh) {
			this.el.on('click', this.onClick, this);
			this.el.first().on('load', function () {
				this.el.unmask.defer(299, this.el);
			}, this);
		}
	},
	onClick: function (e) {
		this.el.mask('loading…');
		this.el.first().dom.src = this.src + '?' + Math.random();
	}
});
Ext.reg('qrcodeImage', Ext.ux.qrcodeImage);

Ext.ux.CommObject = Ext.extend(Ext.BoxComponent, {
	autoEl: {
		codebase: 'MSCOMM32.OCX',
		type: "application/x-itst-activex",
		clsid: '{648A5600-2C6E-101B-82B6-000000000014}',
		tag: 'object'
	},
	initComponent: function () {
		Ext.ux.CommObject.superclass.initComponent.call(this);
	}
});
Ext.reg('commobject', Ext.ux.CommObject);

Ext.ux.CommParam = Ext.extend(Ext.BoxComponent, {
	autoEl: {
		tag: 'param',
		name: 'paramna',
		value: 'paramva'
	},
	initComponent: function () {
		Ext.ux.CommParam.superclass.initComponent.call(this);
	}
});
Ext.reg('commparam', Ext.ux.CommObject);

// 支持双击的Textfield.
Ext.form.DBCTextField = Ext.extend(Ext.form.TextField, {
	initComponent: function () {
		Ext.form.TextField.superclass.initComponent.call(this);
		this.addEvents('autosize', 'keydown', 'keyup', 'keypress', 'dblclick');
	},
	initEvents: function () {
		Ext.form.TextField.superclass.initEvents.call(this);
		if (this.validationEvent == 'keyup') {
			this.validationTask = new Ext.util.DelayedTask(this.validate, this);
			this.mon(this.el, 'keyup', this.filterValidation, this);
		} else if (this.validationEvent !== false) {
			this.mon(this.el, this.validationEvent, this.validate, this, {
				buffer: this.validationDelay
			});
		}
		if (this.selectOnFocus || this.emptyText) {
			this.on('focus', this.preFocus, this);

			this.mon(this.el, 'mousedown', function () {
				if (!this.hasFocus) {
					this.el.on('mouseup', function (e) {
						e.preventDefault();
					}, this, {
						single: true
					});
				}
			}, this);

			if (this.emptyText) {
				this.on('blur', this.postBlur, this);
				this.applyEmptyText();
			}
		}
		if (this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Ext.form.VTypes[this.vtype + 'Mask']))) {
			this.mon(this.el, 'keypress', this.filterKeys, this);
		}
		if (this.grow) {
			this.mon(this.el, 'keyup', this.onKeyUpBuffered, this, {
				buffer: 50
			});
			this.mon(this.el, 'click', this.autoSize, this);
		}
		if (this.enableKeyEvents) {
			this.mon(this.el, 'keyup', this.onKeyUp, this);
			this.mon(this.el, 'keydown', this.onKeyDown, this);
			this.mon(this.el, 'keypress', this.onKeyPress, this);
		}
		this.mon(this.el, 'dblclick', this.onDblClick, this);
	},

	// private
	onDblClick: function (e) {
		//alert('double click on textfield...');
		//showWinForTextFieldToBigger(this);
	}
});
Ext.reg('dbctextfield', Ext.form.DBCTextField);

// ID卡TextField
Ext.form.IDCTextField = Ext.extend(Ext.form.DBCTextField, {
	onDblClick: function (com_obj) {
		/* TODO 添加读卡支持
		var MSComm;
		if (Ext.isIE) {
		MSComm = document.getElementById("MSComm_IE");
		}
		else {
		MSComm = document.getElementById("MSComm_FF");
		}
		alert(MSComm.PortOpen);
		*/
		alert('请读卡......');
		this.setValue('无法读卡...');
	}
});
Ext.reg('infra_idcard_textfield', Ext.form.IDCTextField);

Ext.ux.TbarSeparator = Ext.extend(Ext.Toolbar.Separator, {
	menuid: '',
	xtype: 'tbseparator',
	initComponent: function () {
		ishidden = (this.menuid != '' && CurrentUserMenuPermission.data.keys.indexOf(this.menuid) < 0 );
		Ext.apply(this, {
			//hidden : ishidden
			hidden : false //暂时放开所有按钮
		});
	}
});
Ext.reg('tbarseparator', Ext.ux.TbarSeparator);

// Tbar菜单按钮
// Menuid 与用户授权判断
Ext.ux.TbarMenuBtn = Ext.extend(Ext.Button, {
	menuid: '',
	xtype: "tbbutton",
	minWidth: 80,
	cls: "x-btn-text-icon",
	initComponent: function () {
		ishidden = this.hidden || (this.menuid != '' && CurrentUserMenuPermission.data.keys.indexOf(this.menuid) < 0 );
		Ext.apply(this, {
			hidden : ishidden
			//hidden : false //暂时放开所有按钮
		});
	}
});
Ext.reg('tbarmenubtn', Ext.ux.TbarMenuBtn);

// Tbar按钮
Ext.ux.TbarButton = Ext.extend(Ext.ux.TbarMenuBtn, {
	xtype: "tbarmenubtn",
	text: "按钮",
	minWidth: 80,
	cls: "x-btn-text-icon",
});
Ext.reg('tbarButton', Ext.ux.TbarButton);

// Tbar添加按钮
Ext.ux.TbarAddBtn = Ext.extend(Ext.ux.TbarMenuBtn, {
	xtype: "tbarmenubtn",
	ref: '../addBtn',
	text: "添加",
	icon: "/Content/icons/add.png"
});
Ext.reg('tbaraddbtn', Ext.ux.TbarAddBtn);

// Tbar删除按钮
Ext.ux.TbarDelBtn = Ext.extend(Ext.ux.TbarMenuBtn, {
	xtype: "tbarmenubtn",
	ref: '../delBtn',
	disabled: true,
	text: "删除",
	icon: "/Content/icons/delete.png"
});
Ext.reg('tbardelbtn', Ext.ux.TbarDelBtn);

// Tbar删除按钮
Ext.ux.TbarEditBtn = Ext.extend(Ext.ux.TbarMenuBtn, {
	xtype: 'tbarmenubtn',
	ref: '../editBtn',
	text: '修改',
	icon: "/Content/icons/pencil.png",
	disabled: true
});
Ext.reg('tbareditbtn', Ext.ux.TbarEditBtn);

// 取消按钮
Ext.ux.CancelBtn = Ext.extend(Ext.ux.TbarMenuBtn, {
	text: "退出",
	icon: "../Content/icons/door_open.png"
});
Ext.reg('btncancle', Ext.ux.CancelBtn);

// 提交按钮
Ext.ux.SubmitBtn = Ext.extend(Ext.ux.TbarMenuBtn, {
	text: "提交",
	icon: "../Content/icons/accept.png"
});
Ext.reg('btnsubmit', Ext.ux.SubmitBtn);

// 查询按钮
Ext.ux.QueryBtn = Ext.extend(Ext.ux.TbarMenuBtn, {
	text: "查询",
	icon: "../Content/icons/zoom.png"
});
Ext.reg('tbarqrybtn', Ext.ux.QueryBtn);

// 打印按钮
Ext.ux.PrintBtn = Ext.extend(Ext.ux.TbarMenuBtn, {
	text: "打印",
	icon: "../Content/icons/printer.png"
});
Ext.reg('tbarprintbtn', Ext.ux.PrintBtn);

// 重置按钮
Ext.ux.ResetBtn = Ext.extend(Ext.ux.TbarMenuBtn, {
	text: "重置",
	ref: '../resetBtn',
	icon: "../Content/icons/arrow_rotate_anticlockwise.png"
});
Ext.reg('tbarresetbtn', Ext.ux.ResetBtn);

// 选择按钮
Ext.ux.SelectRetBtn = Ext.extend(Ext.ux.TbarMenuBtn, {
	text: "选择",
	ref: '../selectRet',
	icon: "../Content/icons/plugin_add.png"
});
Ext.reg('tbarselectRetbtn', Ext.ux.SelectRetBtn);

// 查询输入框
Ext.ux.TbarQryTextField = Ext.extend(Ext.form.TextField, {
	width: 120,
	id: 'q_query',
	ref: '../q_query'
});
Ext.reg('tbarQryTxt', Ext.ux.TbarQryTextField);

// 查询重置按钮
Ext.ux.SearchResetBtn = Ext.extend(Ext.ux.TbarMenuBtn, {
	text: "查询重置",
	icon: "../Content/icons/arrow_rotate_anticlockwise.png"
});

// 重写一下button让它disable后好看点
// Ext.override(Ext.Button, {
// disabledClass : ''
// });
// 用于RowEditer判读编辑只读项 约定每个store必须有字段Id才有用
if (Ext.grid.Column) {
	Ext.apply(Ext.grid.Column.prototype, {
		updateEdit: true
	});
}

// 基本comboBox
Ext.ux.BaseComboBox = Ext.extend(Ext.form.ComboBox, {
	emptyText: "请选择...",
	mode: 'local',
	forceSelection: true,
	triggerAction: 'all',
	typeAhead: true,
	selectOnFocus: true,
	allowBlank: false
});
Ext.reg('basecombo', Ext.ux.BaseComboBox);


Ext.apply(Ext.form.VTypes, {
	// 增加密码的验证
	password: function (val, field) {
		if (field.initialPassField) {
			var pwd = Ext.getCmp(field.initialPassField);
			if (val == pwd.getValue()) {
				pwd.clearInvalid();
				return true;
			} else {
				return false;
			}
		}
		return true;
	},

	passwordText: '两次输入不一致，请重新输入'
});

Ext.apply(Ext.form.VTypes, {
	// 增加大小比较的验证
	compareNum: function (val, field) {
		if (field.initialNumField) {
			var otherField = Ext.getCmp(field.initialNumField);

			this.compareNumText = field.compareNumText;
			if (otherField.getValue() != "") {
				if (field.maxOrMin == "max") {
					if (val >= otherField.getValue()) {
						otherField.clearInvalid();
						return true;
					} else {
						return false;
					}
				} else if (field.maxOrMin == "min") {
					if (val <= otherField.getValue()) {
						otherField.clearInvalid();
						return true;
					} else {
						return false;
					}
				}
			} else {
				return false;
			}
		}
		return true;
	}
});

//cookie的时间
Ext.state.Manager.setProvider(
	new Ext.state.CookieProvider({
		expires: new Date(new Date().getTime() + (1000 * 60 * 60 * 24 * 365))
	})
);
